<template>
  <div>
    <div style="padding: 5px 0">
      <el-input v-model="text" @keyup.enter.native="load" style="width: 200px"> <i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-button @click="add" type="primary" size="mini" style="margin: 10px">新增</el-button>
      <el-button @click="delBatchHandle" :disabled="btnChangeEnable" type="danger" size="mini" style="margin: 10px">批量删除</el-button>
    </div>
    <el-table :data="tableData"  @selection-change="changeFun" border stripe style="width: 100%">
      <el-table-column type="selection" width="55" v-model="checkBoxData"></el-table-column>
      <el-table-column prop="id" label="ID" width="100" sortable> </el-table-column>
      <el-table-column prop="name" label="品牌名称"></el-table-column>
      <el-table-column label="品牌logo" width="300">
        <template slot-scope="scope">
          <el-image style="width: 300px; height: 100px" :src="scope.row.logoUrl" :preview-src-list="[scope.row.logoUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="slogan" label="品牌宣传词"></el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle  @click="edit(scope.row)"></el-button>
          <el-popconfirm
              @confirm="del(scope.row.id)"
              title="确定删除？"
          >
            <el-button type="danger" icon="el-icon-delete" circle slot="reference" style="margin-left: 10px"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[2, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 弹窗   -->
    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false">
      <el-form :model="entity">
        <el-form-item label="品牌名称" label-width="150px">
          <el-input v-model="entity.name" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="150px">
          <el-upload :action="`http://${filePath}:9999/files/upload`" :on-success="fileSuccessUpload" :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="品牌宣传词" label-width="150px">
          <el-input v-model="entity.slogan" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getUser} from "@/utils/storage";
import {delBatchCompany} from "@/api/company";
import {delBatchBrands, getPageBrand, removeBrand, saveBrand, updateBrand} from "@/api/brand";
import {proFilePath,devFilePath} from "@/utils/filePath";
// const url = "/api/category"

export default {
  name: "Brand",
  data() {
    return {
      fileList: [],
      options: [],
      text: '',
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      entity: {},
      total: 0,
      dialogFormVisible: false,
      checkBoxData: [],
      btnChangeEnable: true, // 批量删除禁用,
      filePath:proFilePath
    };
  },
  created() {
    this.user = getUser()
    this.$emit('user', this.user);
    this.load()
  },
  methods: {
    changeFun(val) {
      console.log(val)
      this.checkBoxData = val;
      if(val == ''){
        this.btnChangeEnable = true;
      } else {
        this.btnChangeEnable = false;
      }
    },
    fileSuccessUpload(res) {
      this.entity.logoUrl = `http://${this.filePath}:9999/files/` + res.data;
      this.fileList = [res.data]
      console.log(res)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
       getPageBrand({
         pageNum: this.pageNum,
         pageSize: this.pageSize,
         name: this.text
       }).then(res => {
          this.tableData = res.data.records || []
          this.total = res.data.total
       })
    },
    add() {
      this.entity = {}
      this.fileList = []
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.fileList = []
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.id) {
        saveBrand(this.entity).then(res => {
           if (res.code === '0') {
             this.$message({
               type: "success",
               message: "操作成功"
             })
           } else {
             this.$message({
               type: "error",
               message: res.msg
             })
           }
           this.load()
           this.dialogFormVisible = false
        })
      } else {
        updateBrand(this.entity).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "操作成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()
          this.dialogFormVisible = false
        });
      }
    },
    del(id) {
      removeBrand(id).then(res => {
        this.$message({
          type: "success",
          message: "操作成功"
        })
        this.load()
      })
    },
    delBatchHandle() {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 解析checkBoxData中的id值，也可以解析其他包含的数据
        let ids = this.checkBoxData.map(item=>item.id)
        console.log(this.checkBoxData)
        delBatchBrands(ids).then((result) => {
          if (result.code == '0') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.load()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
  },
};
</script>

<style scoped>
</style>
